package yannan.voiplock4ble.utils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ImageView;

import com.yongchun.library.view.ImageSelectorActivity;

import org.greenrobot.eventbus.EventBus;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import yannan.voiplock4ble.base.MyApplication;
import yannan.voiplock4ble.bean.CommonResponse;
import yannan.voiplock4ble.net.NetUtils;
import yannan.voiplock4ble.net.base.NetworkCallBack;


/**
 * Created by PC on 2016/11/29.\
 * result的返回值是66
 */
public class PhotoViewUtils {

    static String headUrl;

    public static String getHeadUrl() {
        return headUrl;
    }

    /**
     * 开启裁剪图片
     *
     * @param activity
     */
    public static void startPicture(Activity activity) {
        ImageSelectorActivity.start(activity, 1, ImageSelectorActivity.MODE_SINGLE, true, true, true);
    }

    /**
     * 上传图片，并回显
     *
     * @param context
     * @param imgHeadPath 图片本地路径
     * @param userId      用户id
     * @param imageView   回显的imageView
     */
    public static void upLoadHead(final Context context, final String imgHeadPath, String userId, final ImageView imageView) {

        final ProgressDialog progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

        MultipartBody.Part body1 = prepareFilePart("excelFile", imgHeadPath);
        NetUtils.getNetworkService().upLoadFile(userId, body1).enqueue(new NetworkCallBack<CommonResponse>() {
            @Override
            public void onSuc(CommonResponse resBody) {
                progressDialog.cancel();
                if (resBody.returnValue != null) {
                    if (MyApplication.getInterface().userBean != null) {
                        headUrl = resBody.returnValue;
                        MyApplication.getInterface().userBean.headImageUrl = resBody.returnValue;
                    }
                    String url = NetUtils.Base_Url_Down + resBody.returnValue;
//                    NetUtils.loadImage(context, imageView, 0, url);
                    NetUtils.loadCircleImage(context, imageView, 0, imgHeadPath);
                    EventBus.getDefault().post("updateHeadImg");
                }

            }

            @Override
            public void onFail(String msg) {
                progressDialog.cancel();
            }
        });
    }

    private static final String MULTIPART_FORM_DATA = "multipart/form-data";

    @NonNull
    private RequestBody createPartFromString(String descriptionString) {
        return RequestBody.create(
                MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
    }

    @NonNull
    private static MultipartBody.Part prepareFilePart(String partName, String path) {
        File file = new File(path);
        // 为file建立RequestBody实例
        RequestBody requestFile =
                RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);

        // MultipartBody.Part借助文件名完成最终的上传
        return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }


}
